package com.riamm.ui {
  import com.riamm.events.ComponentDrawEvent;
  import com.riamm.interfaces.IDestroy;

  import flash.display.Sprite;
  import flash.events.Event;
  /**
   * @author Saber
   * This Base human, base component.
   * They form Heroic Age, have a variety ability and even magic. now we like god,
   * can create them and give them the ability or magic.
   * Maybe you need a factory..
   */
  public class Component extends Sprite implements IDestroy {
    //==========================================================================
    //  Constructor
    //==========================================================================
    public function Component(xpos:int = 0, ypos:int = 0) {
      move(xpos, ypos);
      init();
    }
    //==========================================================================
    //  Class properties
    //==========================================================================
    protected var _width              :Number = 0;
    protected var _height             :Number = 0;

    protected var _disable            :Boolean = false;
    //==========================================================================
    //  Class Intreface implement methods
    //==========================================================================
    public function destroy():void {}
    public function get disable():Boolean {
      return _disable;
    }
    public function set disable(value:Boolean):void {}
    //==========================================================================
    // public methods
    //==========================================================================
    public function move(xpos:int, ypos:int):void {
      x = xpos;
      y = ypos;
    }
    public function setSize(w:Number, h:Number):void {
      this.width = w;
      this.height = h;
      invalidata();
    }
    //==========================================================================
    // protected methods
    //==========================================================================
    protected function init():void {
      createFeature();
      invalidata();
    }
    protected function invalidata():void {
      if (!hasEventListener(Event.ENTER_FRAME))
        addEventListener(Event.ENTER_FRAME, senceUpdateHandler);
    }
    protected function createFeature():void {}
    protected function draw():void {
      dispatchEvent(new ComponentDrawEvent(ComponentDrawEvent.DRAW));
    }
    //==========================================================================
    //  Overridden methods
    //==========================================================================
    override public function set width(value:Number):void {
      _width = value;
    }
    override public function set height(value:Number):void {
      _height = value;
    }
    override public function get width():Number {
      return _width;
    }
    override public function get height():Number {
      return _height;
    }
    override public function set x(value:Number):void {
      super.x = Math.round(value);
    }
    override public function set y(value:Number):void {
      super.y = Math.round(value);
    }
    //==========================================================================
    //  Event handlers
    //==========================================================================
    private function senceUpdateHandler(event:Event):void {
      removeEventListener(Event.ENTER_FRAME, senceUpdateHandler);
      draw();
    }
  }
}
